<?php
class OrderAction extends SecurityAction {
	public $uploadPath = '/files/order/';
	public $orderStatus = array (
			'0' => '取消订单',
			'1' => '未支付',
			'3' => '电子版文件错误',
			'4' => '取消订单',
			'7' => '等待印刷中',
			'10' => '已发货',
			'11' => '已完成' 
	);
	function _initialize() {
		$this->assign ( 'rid', session('info.rid') );
		$this->assign ( 'orderStatus', $this->orderStatus );
	}
	function cancelOrder() {
		$ob = M ( "csl_order" );
		$omap = array (
				'order_no' => $_GET ['order_no'],
				'order_status' => '1' 
		);
		$ob->where ( $omap )->setField ( 'order_status', '0' );
		$this->redirect ( '/Admin/Order/olist/?p=' . $_GET ['p'] );
		
		return;
	}
	function payorder() {
		$ou = M ( "csl_user" );
		$map = array (
				'uid' => session ( 'info.uid' ) 
		);
		$info = $ou->where ( $map )->find ();
		
		$ob = M ( "csl_order" );
		$omap = array (
				'order_no' => $_GET ['order_no'] 
		);
		$oinfo = $ob->where ( $omap )->find ();
		
		if ($oinfo ['order_status'] != '1') {
			$this->redirect ( '/Admin/Order/olist' );
			return;
		}
		
		if ($info ['inventory'] < $oinfo ['number']) {
			$this->error ( "账户支付码不足。" );
			return;
		}
		
		$newInventory = $info ['inventory'] - $oinfo ['number'];
		$return = $ou->where ( $map )->setField ( 'inventory', $newInventory );
		if ($return) {
			LogTransactionModel::ModifyInventory ( $info ['uid'], $oinfo [order_no], '订单支付', "使用印刷码[{$oinfo['number']}个]支付订单[{$oinfo[order_no]}] 成功" );
			
			$op = M ( "csl_print_code" );
			
			$insert ['uid'] = session ( 'info.uid' );
			$insert ['code'] = date ( 'YmdHis' ) . strtoupper ( md5 ( session ( 'info.uid' ) . rand ( 10000, 999999 ) ) );
			$insert ['order_no'] = $oinfo [order_no];
			$op->add ( $insert );
		} else {
			LogTransactionModel::ModifyInventory ( $info ['uid'], $oinfo [order_no], '订单支付', "使用印刷码[{$oinfo['number']}个]支付订单[{$oinfo[order_no]}] 失败" );
		}
		
		// $ob->where($omap)->setField('order_status','7');
		$data ["order_status"] = "7";
		$data ["check"] = "1";
		$ob->where ( $omap )->save ( $data );
		
		$this->redirect ( '/Admin/Order/olist' );
		return;
	}
	function olist() {
		
		$order_no = $_GET ["order_no"];
		$ob = M ( "csl_order" );
		
		if($_GET['savenote']=='Y'){
			$data['note'] = $_POST['note'];
			$ob->where(array('order_no'=>$_POST['order_no']))->save($data);
			echo json_encode(array('status'=>'1','message'=>"保存成功"));
			return ;
		}
		// 上传文件
		if ($_POST) {
			$upload = $this->uploadCard ();
			if ($upload) {
				$add ['order_img'] = $upload;
				$ob->where ( array (
						'order_no' => $_POST ["order_no"] 
				) )->save ( $add );
				$this->success ( "上传文件成功", '/Admin/Order/olist/p/' . $_GET ['p'] . "/dingwei/" . $_POST ["order_no"] );
				exit ();
			} else {
				$this->error ( "文件上传失败。正在转跳...", '/Admin/Order/olist/p/' . $_GET ['p'] );
				exit ();
			}
		}
		
		$sql [] = array (
				'order_status' => array (
						'NEQ',
						'0' 
				) 
		);
		if (session ( 'info.uid' ) != '1') {
			$ou = new UserModel ();
			$ulist = $ou->getAllChildrenUser ( session ( 'info.uid' ) );
			$sql [] = array (
					'uid' => array (
							'in',
							$ulist 
					) 
			);
		} else {
		}
		
		if (! empty ( $_GET ['order_no'] )) {
			$sql [] = array (
					'order_no' => $_GET ['order_no'] 
			);
		}
		
		if (! empty ( $_GET ['order_status'] )) {
			$sql [] = array (
					'order_status' => $_GET ['order_status'] 
			);
		}
		
		$cpp = 30; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $sql )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		
 		$list = $ob->where ( $sql )->order ( array (
 				'oid' => 'desc' 
 		) )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
 		
 		
 		//整理购物车的产品
 		$order_no_list = takearr($list,'order_no');
		$cart = $ob->table("csl_shopping_cart b")
				     ->field('b.order_no,b.pid,b.price,b.color_name,b.quantity,c.pname')
				     ->join("csl_products c on b.pid = c.pid")//连接产品表
				     ->where("b.order_no in $order_no_list")
				     ->select();
		if(!empty($cart)) {
			$temp = array();
			foreach($cart as $k=>$v) {
				$temp[$v['order_no']][] = $v;
			}
			$cart = $temp;
		}
		
		$this->assign ( "list", $list );
		$this->assign ( "cart", $cart );
		$this->assign ( 'show', $show ); // 赋值分页输出
		$this->display ( "Order/olist" );
	}
	
	/*
	 * 新增订单 - 门店账号专用
	 */
	function newOrder() {
		if ($_GET ['delete']) {
			$ob = M ( "csl_order" );
			$info = $ob->where ( array (
					'order_no' => $_GET ['delete'] 
			) )->find ();
			if (in_array ( $info ['order_status'], array (
					1,
					2,
					3,
					4,
					5,
					6 
			) )) {
				$ob->where ( array (
						'order_no' => $_GET ['delete'] 
				) )->save ( array (
						'order_status' => '0' 
				) );
				$this->redirect ( '/Admin/Order/olist/' );
				return;
			}
		}
		if ($_POST) {
			if ($_POST ['oid'] == '') { // 新增
				$no = createOrderNo ();
				$randNum = rand ( 1000, 9999 );
				$_POST ['order_no'] = $no . $randNum; // 订单号
			}
			
			$_POST ['uid'] = session ( 'info.uid' ); // 下单人
			$_POST ['order_item_price'] = 44;
			$_POST ['order_price'] = $_POST ['number'] * $_POST ['order_item_price']; // 订单总金额
			
			if (empty ( $_POST ['order_status'] )) {
				$_POST ['order_status'] = '1'; // 订单状态
			}
			$upload = $this->uploadCard ();
			if ($upload) {
				$_POST ['order_img'] = $upload;
			}
			
			$_POST ['ctime'] = date ( 'Y-m-d H:i:s', time () ); // 生成时间
			$_POST ['order_coupon_price'] = 0;
			$_POST ['order_shipping_cost'] = 0;
			$data = $_POST;
			$ob = M ( "csl_order" );
			$obD = M ( "csl_order_detail" );
			if ($_POST ['oid'] != '') {
				$ob->where ( array (
						'oid' => $_POST ['oid'] 
				) )->save ( $data );
			} else {
				$ob->add ( $data );
			}
			
			$this->redirect ( '/Admin/Order/olist' );
			return;
		}
		
		if ($_GET ["order_no"]) {
			$ob = M ( "csl_order" );
			$list = $ob->where ( array (
					"order_no" => $_GET ["order_no"] 
			) )->find ();
			
			$obD = M ( "csl_order_detail" );
			$listDetail = $obD->where ( array (
					"order_no" => $_GET ["order_no"] 
			) )->find ();
			
			$this->assign ( "info", $list );
			$this->assign ( "infoDetail", $listDetail );
		}
		
		$this->display ( 'Order/newOrder' );
	}
	
	/*
	 * 订单维护进度 - 超级管理员、工厂
	 */
	function detail() {
		if (empty ( $_GET ["order_no"] )) {
			$this->redirect ( '/Admin/Order/olist/' );
			return;
		}
		
		$ob = M ( "csl_order" );
		$info = $ob->where ( array (
				"order_no" => $_GET ["order_no"] 
		) )->find ();
		
		if ($_POST) {
			if ($info ['order_status'] == '0') {
				$this->redirect ( '/Admin/Order/olist/', null, 3, "订单已经取消无法更改。正在转跳..." );
			}
			
			if ($info ['order_status'] != '1' && $info ['order_status'] != '3') {
				
				$upload = $this->uploadCard ();
				if ($upload) {
					$add ['order_img'] = $upload;
					$ob->where ( array (
							'order_no' => $_GET ["order_no"] 
					) )->save ( $add );
					$this->success ( "上传文件成功", '/Admin/Order/olist/' );
				} else {
					$this->error ( "文件上传失败。正在转跳...", '/Admin/Order/olist/' );
				}
			}
			unset ( $_POST ['order_status'] );
			unset ( $_POST ['order_price'] );
			unset ( $_POST ['order_item_price'] );
			unset ( $_POST ['number'] );
			$ob->where ( array (
					'order_no' => $_GET ["order_no"] 
			) )->save ( $_POST );
			$this->redirect ( '/Admin/Order/olist/' );
			return;
		}
		
		// 操作日志
		$db = M ( 'log_transaction' );
		$map ['order_no'] = $_GET ["order_no"];
		$log = $db->where ( $map )->order ( array (
				'id' => 'desc' 
		) )->limit ( 0, 100 )->select ();
		$this->assign ( "log", $log );
		
		$this->assign ( "info", $info );
		$this->display ( 'Order/detail' );
	}
	
	// 待支付订单
	function wolist() {
		$ob = M ( "csl_order" );
		$sql [] = array (
				'order_status' => 1 
		);
		
		$ou = new UserModel ();
		$ulist = $ou->getAllChildrenUser ( session ( 'info.uid' ) );
		
		$sql [] = array (
				'uid' => array (
						'in',
						$ulist 
				) 
		);
		
		$cpp = 10; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $sql )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		
		$list = $ob->where ( $sql )->order ( array (
				'oid' => 'desc' 
		) )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		
		$this->display ( 'Order/wolist' );
	}
	
	// 工厂订单列表可以查看状态3以上所有订单
	function factoryOrder() {
		$ob = M ( "csl_order" );
		$sql [] = array (
				'order_status' => array (
						'egt',
						'3' 
				) 
		);
		if (! empty ( $_GET ['order_no'] )) {
			$sql [] = array (
					'order_no' => $_GET ['order_no'] 
			);
		}
		$cpp = 10; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $sql )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		
		$list = $ob->where ( $sql )->order ( array (
				'oid' => 'desc' 
		) )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		
		$this->display ( 'Order/factoryOrder' );
	}
	function factoryOrderDetail() {
		if (empty ( $_GET ["order_no"] )) {
			$this->redirect ( '/Admin/Order/factoryOrder/' );
			return;
		}
		
		$ob = M ( "csl_order" );
		
		if ($_POST) {
			$data ['order_status'] = $_POST ['order_status'];
			$ob->where ( array (
					'order_no' => $_GET ["order_no"] 
			) )->save ( $data );
			$this->redirect ( '/Admin/Order/factoryOrder/' );
			return;
		}
		
		$list = $ob->where ( array (
				"order_no" => $_GET ["order_no"] 
		) )->find ();
		
		// 操作日志
		$db = M ( 'log_transaction' );
		$map ['order_no'] = $_GET ["order_no"];
		$log = $db->where ( $map )->order ( array (
				'id' => 'desc' 
		) )->limit ( 0, 100 )->select ();
		$this->assign ( "log", $log );
		
		$this->assign ( "info", $list );
		$this->display ( 'Order/factoryOrderDetail' );
	}
	
	// 下载文件
	function download() {
		$ob = M ( "csl_order" );
		$info = $ob->where ( array (
				"order_no" => $_GET ['order_no'] 
		) )->find ();
		
		$filename = APP_PATH . $info ["order_img"];
		$fileinfo = pathinfo ( $filename );
		
		$file = fopen ( $filename, "rb" ) or die ( "Unable to open file" );
		Header ( "Content-type:application/octet-stream " );
		Header ( "Accept-Ranges:  bytes" );
		Header ( "Content-Disposition:  attachment;filename=" . $fileinfo ["basename"] );
		$contents = "";
		while ( ! feof ( $file ) ) {
			$contents .= fread ( $file, 8192 );
		}
		echo $contents;
		fclose ( $file );
	}
	function uploadCard() {
		if (isset ( $_FILES )) {
			if (! is_dir ( APP_PATH . '/files/' )) {
				mkdir ( APP_PATH . '/files/', 0755, true );
			}
			if (! is_dir ( APP_PATH . '/files/order/' )) {
				mkdir ( APP_PATH . '/files/order/', 0755, true );
			}
			// 文件上传配置
			import ( 'ORG.Net.UploadFile' );
			$upload = new UploadFile (); // 实例化上传类
			$upload->maxSize = 10000000; // 设置附件上传大小;默认为-1，不限制上传大小
			$upload->allowExts = array (
					'jpg',
					'gif',
					'png',
					'jpeg',
					'pdf' 
			); // 设置附件上传类型
			$upload->allowTypes = array (
					'image/jpg',
					'image/jpeg',
					'image/pjpeg',
					'image/png',
					'image/gif',
					'application/pdf' 
			);
			$upload->savePath = APP_PATH . $this->uploadPath; // 设置附件上传目录
			$upload->saveRule = $_POST ['order_no'];
			$upload->uploadReplace = true;
			// 开始上传
			if (! $upload->upload ()) {
				// $this->error($upload->getErrorMsg());
				return false;
			} else {
				$info = $upload->getUploadFileInfo ();
				// $_POST['order_img'] = $this->uploadPath . $info[0]['savename'];
				return $this->uploadPath . $info [0] ['savename'];
			}
		}
	}
	
	//创建体验订单
	function newExperience() {
		$db = D(Experience);
		
		if ($_POST) {
			if($_POST['dict_id']!='') {
				$dict = $db->getkind($_POST['dict_id']);
				$_POST['order_product_id'] = $dict['id'];
				$_POST['order_product_price'] = $dict['code'];
				$_POST['product_product_name'] = $dict['value'];
			}
				
			$_POST ['uid'] = session ( 'info.uid' ); // 下单人
			$_POST ['order_price'] = $_POST ['count'] * $_POST ['order_product_price']; // 订单总金额
				
			if (empty ( $_POST ['order_status'] )) {
				$_POST ['order_status'] = '1'; // 订单状态
			}
			
				
			$_POST ['ctime'] = date ( 'Y-m-d H:i:s', time () ); // 生成时间
			$_POST ['order_coupon_price'] = 0;
			$_POST ['order_shipping_cost'] = 0;
			$data = $_POST;
			if($_POST ['osid'] != '') {
				$db->where(array (
					'osid' => $_POST ['osid']
				))->save ( $data );
			} else {
				$osid = $db->add ( $data );
				
				//形成订单后生成订单号
				$draw = array();
				$draw['osid'] = $osid;
				$order_no = $osid.rand(100000,999999);//订单号
				$db->where($draw)->setField('order_no',$order_no);
			}
				
			$this->redirect ( '/Admin/Order/elist' );
			return;
		}
		
		$kind = $db->getkind();
		
		$this->assign('kind',$kind);
		$this->display ('Order/newExperience');
	}
	
	//体验产品订单列表
	function elist() {
		$db = D('Experience');
		
		$sql [] = array (
				'order_status' => array (
						'NEQ',
						'0'
				)
		);
		if (session ( 'info.uid' ) != '1') {
			$ou = new UserModel ();
			$ulist = $ou->getAllChildrenUser ( session ( 'info.uid' ) );
			$sql [] = array (
					'uid' => array (
							'in',
							$ulist
					)
			);
		}
		
		if (! empty ( $_GET ['osid'] )) {
			$sql [] = array (
					'osid' => $_GET ['osid']
			);
		}
		
		if (! empty ( $_GET ['order_status'] )) {
			$sql [] = array (
					'order_status' => $_GET ['order_status']
			);
		}
	
		$cpp = 30; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $db->where ( $sql )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
	
		$list = $db->where ( $sql )->order ( array (
				'osid' => 'desc'
		) )->limit ( $Page->firstRow . ',' . $Page->listRows )->select();
	
		$this->assign("list",$list);
		$this->assign('show',$show); // 赋值分页输出
	
		$this->display("Order/elist");
	}
	
	
	//体验产品订单修改为完成状态
	function finish_e_order() {
		$db = D('Experience');
		
		$osid = $_GET['osid'];
		$p = $_GET['p'];
		
		$map = array();
		$map['osid'] = $osid;
		$map['order_status'] = 7;//限制7状态的订单才能修改为11状态
		
		$db->where($map)->setField('order_status','11');
		$this->redirect ( '/Admin/Order/elist/p/'.$p );
	}
	
	
	
	
	
	
}

?>